<template>
	<el-dialog v-model="visible" title="证件信息" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="220" :label-position="'top'">
			<el-form-item label="证件类型" prop="idCrdTpCd">
				<fast-select v-model="dataForm.idCrdTpCd" style="width: 100%" dict-type="mpi_id_crd_cgy_cd"
					placeholder="证件类型" clearable filterable></fast-select>
			</el-form-item>
			<el-form-item label="证件号" prop="idCrdNo">
				<el-input v-model="dataForm.idCrdNo" maxlength="50" placeholder="请输入证件号"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" :loading="btnLoading" @click="subFormHandle">确定</el-button>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, nextTick } from 'vue'
import { saveCert } from '@/api/patient-primary/patient-mana'
import { ElMessage } from 'element-plus'
const visible = ref(false)
const emit = defineEmits(['close'])
const btnLoading = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	idCrdNo: '',
	idCrdTpCd: ''
})

const dataRules = ref({
	idCrdNo: [
		{
			required: true,
			message: '证件号不能为空',
			trigger: 'blur'
		}
	],
	idCrdTpCd: [
		{
			required: true,
			message: '证件类型不能为空',
			trigger: 'change'
		}
	]
})

const init = (data, empiId) => {
	visible.value = true
	btnLoading.value = false

	nextTick(() => {


		if (dataFormRef.value) {
			dataFormRef.value.resetFields()
		}

		dataForm.empi = empiId

		if (data) {
			Object.assign(dataForm, data)
		} else {
			dataForm.id = ''
		}
	})
}

const subFormHandle = () => {
	dataFormRef.value.validate(valid => {
		if (!valid) {
			return false
		}
		btnLoading.value = true

		saveCert(dataForm)
			.then(res => {
				ElMessage.success({
					message: '保存成功',
					duration: 500,
					onClose: () => {
						btnLoading.value = false
						visible.value = false
						emit('close')
					}
				})
			})
			.catch(() => {
				btnLoading.value = false
			})
	})
}

defineExpose({
	init
})
</script>
<style lang="less" scoped></style>
